local M = {}

local options = {
  capabilities = require('cmp_nvim_lsp').default_capabilities(vim.lsp.protocol.make_client_capabilities()),
  lsp_flags = { debounce_text_changes = 150 },
  settings = {
    ['rust-analyzer'] = {
      cargo = {
        autoReload = true,
      },
      checkOnSave = {
        command = 'clippy', -- 使用cargo clippy检查哪些代码可以优化
      },
    },
  },
}

M.setup = function(server)
  local ok_rt, rt = pcall(require, 'rust-tools')
  if not ok_rt then
    vim.notify('Failed to load rust tools, will set up `rust_analyzer` without `rust-tools`.')
    server.setup(options)
  else
    rt.setup({
      server = options,
      dap = {
        adapter = {
          type = 'executable',
          command = 'lldb-vscode',
          name = 'rt_lldb',
        },
      },
    })
  end
end

return M
